<?php
namespace PsiFramework\Applications\AlloMediaUser\Classes\Actions;

import("Applications.AlloMediaUser.Classes.Actions.ShopAction");
import("Classes.Util.HTTP.AuthenticationManager");
import("Classes.Factory.DAOFactory");
import("Plugins.Shop.Classes.Model.Order");

use \PsiFramework\Applications\AlloMediaUser\Classes\Actions\ShopAction;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\InformationPart;
use \PsiFramework\Classes\Util\HTTP\AuthenticationManager;
use \PsiFramework\Classes\Util\Parameters\Action\ActionParameters;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Plugins\Shop\Classes\Model\Order;
use \PsiFramework\Classes\Util\Object\ReferenceObject;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de l'index
 *
 * @package Actions.Pages
 */
class AddServicesToBasketAction extends ShopAction
{

    public function __construct(ActionParameters $actionParameters)
    {
        parent::__construct($actionParameters);
    }

    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     *
     */
    public function execute()
    {
        parent::execute();
        
        $servicesCount = $this->_request->getParameter('countServices');
        for ($i=0; $i<$servicesCount; $i++) {
            $selected = $this->_request->getParameter('checkedTheme_'.$i);
            if (!isNullOrEmptyString($selected)) {
                $responseIdId = $this->_request->getParameter('responseId_'.$i);
                $quantity = $this->_request->getParameter('quantity_'.$i);
                
                if (!isNullOrEmptyString($quantity)) {
                    $responseIdDao = DAOFactory::getDAO('ResponseId');
                    $responseId = $responseIdDao->retrieveByPk($responseIdId);
                    
                    if (!is_null($responseId)) {
                        $samplePart = new InformationPart();
                        $samplePart->setTheme($responseId->getTheme());
                        $samplePart->setProfile($responseId->getEntry()->getCategory()->getProfile());
                        $informationPartDao = DAOFactory::getDAO('InformationPart');
                        $part = $informationPartDao->retrieveByPk($samplePart->getId());
                        
                        $refObject = new ReferenceObject('ResponseId', $responseId->getId());
                        $item = self::getItem($refObject, $part);
                        
                        $order = $this->_response->getAttribute('order');
                        
                        self::addItemToOrder($order, $item, $quantity);
                    }
                }
            }
        }
        
        $this->findForward('success');
    }
}
